package com.zivwu.mvp.pick;


import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.MediaStore;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.zivwu.mvp.pick.list.PhotoPickerActivity;
import com.zivwu.mvp.pick.preview.PhotoPreviewActivity;
import com.zivwu.utils.model.Photo;

import java.util.ArrayList;

public class PhotoPicker {
    public static final int REQUEST_CODE = 233;


    public static final String[] PHOTO_PICKER_PERMISSION = new String[]{Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    public final static String KEY_SELECTED_PHOTOS = "SELECTED_PHOTOS";

    public static final String EXTRA_PHOTO_DATA = "PHOTO_DATA";
    public final static String EXTRA_MAX_COUNT = "MAX_COUNT";
    public final static String EXTRA_OPEN_PREVIEW = "EXTRA_OPEN_PREVIEW";

    public final static String EXTRA_PREVIEW_PHOTO = "EXTRA_PREVIEW_PHOTO";
    public final static String EXTRA_PHOTO_INDEX = "EXTRA_PHOTO_INDEX";


    public static PhotoPickerBuilder builder() {
        return new PhotoPickerBuilder();
    }


    public static class PhotoPickerBuilder {
        private Bundle mPickerOptionsBundle;
        private Intent mPickerIntent;
        private Bundle options;


        public PhotoPickerBuilder() {
            mPickerOptionsBundle = new Bundle();
            mPickerIntent = new Intent();
        }


        public void start(@NonNull AppCompatActivity activity) {
            this.start(activity, REQUEST_CODE);
        }


        public void start(@NonNull final AppCompatActivity activity, final int requestCode) {
            if (isCrop()) {
                goCropPhoto(activity, requestCode, mPickerOptionsBundle, mPickerIntent);
            } else if (isPreView()) {
                goPreviewActivity(activity);
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                String[] descList = new String[]{"拍照", "从手机相册选择"};
                AlertDialog dialog = builder.setTitle("请选择照片来源")
                        .setItems(descList, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                switch (i) {
                                    case 0:
                                        goPickPhoto(activity,
                                                RequestCode.makeRequestCode(requestCode, RequestCode.CAMERA));
                                        dialogInterface.dismiss();
                                        break;
                                    case 1:
                                        goPickPhoto(activity,
                                                RequestCode.makeRequestCode(requestCode, RequestCode.ALBUM));
                                        dialogInterface.dismiss();
                                        break;
                                }
                            }
                        }).create();
                dialog.show();
            }
        }


        private void goCropPhoto(@NonNull AppCompatActivity activity, int requestCode, Bundle mPickerOptionsBundle, Intent mPickerIntent) {
            mPickerOptionsBundle.putBoolean("crop", true);
            mPickerOptionsBundle.putBoolean("scale", true);//保持碧绿
            mPickerOptionsBundle.putBoolean("noFaceDetection", true); // 头像识别
            mPickerOptionsBundle.putString("outputFormat", Bitmap.CompressFormat.JPEG.toString());
            mPickerIntent.putExtras(mPickerOptionsBundle);
            activity.startActivityForResult(mPickerIntent,
                    RequestCode.makeRequestCode(requestCode, RequestCode.CROP)
                    , options);
        }

        private void goPickPhoto(AppCompatActivity activity, int requestCode) {
            mPickerIntent.putExtras(mPickerOptionsBundle);
            if (RequestCode.getMode(requestCode) == RequestCode.ALBUM) {
                mPickerIntent.setClass(activity, PhotoPickerActivity.class);
                activity.startActivityForResult(mPickerIntent, requestCode, options);
            } else {
                mPickerIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                if (mPickerIntent.resolveActivity(activity.getPackageManager()) != null) {
                    activity.startActivityForResult(mPickerIntent, requestCode);
                }

            }

        }


        private void goPreviewActivity(AppCompatActivity activity) {
            mPickerIntent.setClass(activity, PhotoPreviewActivity.class);
            mPickerIntent.putExtras(mPickerOptionsBundle);
            activity.startActivity(mPickerIntent, options);
        }

        private boolean isPreView() {
            ArrayList<Parcelable> list = mPickerOptionsBundle.getParcelableArrayList(EXTRA_PREVIEW_PHOTO);
            return list != null && list.size() > 0;
        }

        public PhotoPickerBuilder closePreviewPhoto() {
            mPickerOptionsBundle.putBoolean(EXTRA_OPEN_PREVIEW, false);
            return this;
        }

        private boolean isCrop() {
            return mPickerIntent.getData() != null;
        }

        public PhotoPickerBuilder setPhotoCount(int photoCount) {
            mPickerOptionsBundle.putInt(EXTRA_MAX_COUNT, photoCount);
            return this;
        }


        public PhotoPickerBuilder setPreviewPhoto(ArrayList<Photo> photoArrayList) {
            mPickerOptionsBundle.putParcelableArrayList(EXTRA_PREVIEW_PHOTO, photoArrayList);
            return this;
        }

        public PhotoPickerBuilder setPhotoIndex(int index) {
            mPickerOptionsBundle.putInt(EXTRA_PHOTO_INDEX, index);
            return this;
        }


        public PhotoPickerBuilder setExtraOutput(Uri uri) {
            mPickerOptionsBundle.putParcelable(MediaStore.EXTRA_OUTPUT, uri);
            return this;
        }

        public PhotoPickerBuilder setOptions(Bundle options) {
            this.options = options;
            return this;
        }

        public Bundle getBundle() {
            return mPickerOptionsBundle;
        }


        public PhotoPickerBuilder setDataAndType(Uri inUri) {
            mPickerIntent.setAction("com.android.camera.action.CROP");
            mPickerIntent.setDataAndType(inUri, "image/*");
            return this;
        }

    }


}
